package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.starlink.model.util.MallPeripheryOrderDetailsConvert;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.MallPeripheryOrderDetailsDtoMapper;
import com.starlink.model.domain.MallPeripheryOrderDetailsDO;
import com.starlink.mapper.MallPeripheryOrderDetailsMapper;
import com.starlink.model.request.MallPeripheryOrderDetailsRequest;
import com.starlink.service.MallPeripheryOrderDetailsService;
import com.starlink.model.vo.MallPeripheryOrderDetailsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.starlink.utils.IDUtils.getId;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class MallPeripheryOrderDetailsServiceImpl extends ServiceImpl<MallPeripheryOrderDetailsMapper, MallPeripheryOrderDetailsDO> implements MallPeripheryOrderDetailsService {
    @Autowired
    private MallPeripheryOrderDetailsMapper mallPeripheryOrderDetailsMapper;

    @Override
    public MallPeripheryOrderDetailsVO getByKey(long id) {
        MallPeripheryOrderDetailsVO mallPeripheryOrderDetailsVO = MallPeripheryOrderDetailsDtoMapper.INSTANCE.do2VO(getById(id));
        return mallPeripheryOrderDetailsVO;
    }

    @Override
    public PageInfo<MallPeripheryOrderDetailsVO> pageQuery(Pageable pageable,MallPeripheryOrderDetailsRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<MallPeripheryOrderDetailsDO> list = mallPeripheryOrderDetailsMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<MallPeripheryOrderDetailsVO> mallPeripheryOrderDetailsVOList = MallPeripheryOrderDetailsConvert.buildMallPeripheryOrderDetailsVoList(list);
        return new PageInfo(mallPeripheryOrderDetailsVOList);
    }

    @Override
    public List<MallPeripheryOrderDetailsVO> listQuery(MallPeripheryOrderDetailsRequest request) {
        List<MallPeripheryOrderDetailsDO> list = mallPeripheryOrderDetailsMapper.selectDOList(MallPeripheryOrderDetailsDtoMapper.INSTANCE.request2DO(request));
        List<MallPeripheryOrderDetailsVO> mallPeripheryOrderDetailsVOS = MallPeripheryOrderDetailsConvert.buildMallPeripheryOrderDetailsVoList(list);
        return mallPeripheryOrderDetailsVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(MallPeripheryOrderDetailsRequest request) {
        request.setId(getId());
        save(MallPeripheryOrderDetailsDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(MallPeripheryOrderDetailsRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(MallPeripheryOrderDetailsDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        mallPeripheryOrderDetailsMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRimOrderDetaile(Long orderId) {
        mallPeripheryOrderDetailsMapper.deleteRimOrderDetaile(orderId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletedByOrderIds(String stringIds) {
        mallPeripheryOrderDetailsMapper.deletedByOrderIds(Convert.toStrArray(stringIds));
    }
}
